package com.hmall.trade.listeners;


import com.hmall.trade.domian.po.Order;
import com.hmall.trade.service.impl.OrderServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@AllArgsConstructor
public class PayStatusListener {

    private final OrderServiceImpl orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "mark.order.pay.queue",durable = "true"),
            exchange = @Exchange(name = "pay.topic",type = ExchangeTypes.TOPIC),
            key = "pay.success"
    ))
    public void listenOrderPay(Long OrderId){
//        //1、查询订单
//        Order order = orderService.getById(OrderId);
//        //2、判断订单状态是否为未支付
//        if(order == null || order.getStatus() != 1){
//            //订单不存在状态异常
//            return;
//        }
//        //3、如果为未支付，标记订单状态为已支付
//        orderService.markOrderPaySuccess(OrderId);

        orderService.lambdaUpdate()
                .set(Order::getStatus, 2)
                .set(Order::getPayTime, LocalDateTime.now())
                .eq(Order::getId, OrderId)
                .eq(Order::getStatus, 1)
                .update();
    }
}
